package cn.getech.data.development.service.impl;

import cn.getech.data.development.model.req.realtimequeue.RealtimeQueueListReq;
import cn.getech.data.intelligence.common.exception.RRException;
import cn.getech.data.intelligence.common.utils.PojoUtils;
import cn.getech.system.center.entity.SysTenantQueue;
import cn.getech.system.center.service.SysTenantQueueService;
import cn.getech.system.center.utils.ShiroUtils;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.getech.data.intelligence.common.utils.PageUtils2;
import cn.getech.data.intelligence.common.utils.Query;

import cn.getech.data.development.mapper.RealtimeQueueMapper;
import cn.getech.data.development.entity.RealtimeQueueEntity;
import cn.getech.data.development.service.RealtimeQueueService;


@Service("realtimeQueueService")
public class RealtimeQueueServiceImpl extends ServiceImpl<RealtimeQueueMapper, RealtimeQueueEntity> implements RealtimeQueueService {

    @Autowired
    private RealtimeQueueMapper realtimeQueueMapper;
    @Autowired
    private SysTenantQueueService sysTenantQueueService;

    @Override
    public PageUtils2<RealtimeQueueEntity> queryPage(Map<String, Object> params) {
        //查询租户对应的队列
        Integer tenantId = ShiroUtils.getTenantId();
        List<Integer> queueIds = null;
        if(null != tenantId){
            List<SysTenantQueue> list = sysTenantQueueService.list(new QueryWrapper<SysTenantQueue>().eq("tenant_id",tenantId));
            if(CollectionUtil.isNotEmpty(list)){
                queueIds = list.stream().map(SysTenantQueue :: getQueueId).collect(Collectors.toList());
            }
        }
        IPage<RealtimeQueueEntity> page = this.page(
                new Query<RealtimeQueueEntity>().getPage(params),
                new QueryWrapper<RealtimeQueueEntity>().in(CollectionUtil.isNotEmpty(queueIds),"queue_id",queueIds)
        );

        return new PageUtils2<RealtimeQueueEntity>(page);
    }

    @Override
    public PageUtils2<RealtimeQueueEntity> queryPage(RealtimeQueueListReq req) {
        Map<String, Object> params = null;
        try {
            params = PojoUtils.getObjectToMap(req);
        } catch (IllegalAccessException e) {
            throw new RRException("对象转map失败");
        }

        return queryPage(params);
    }

    @Override
    public void cheackQueue(Long queueId) {
        if(null == queueId){
            throw new RRException("调度队列不存在！");
        }
        RealtimeQueueEntity realtimeQueueEntity = realtimeQueueMapper.selectById(queueId);
        if(null == realtimeQueueEntity){
            throw new RRException("调度队列不存在！");
        }
    }

}
